/* Copyright 2012 Sebastiaan Schalbroeck - schalbroeck@gmail.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nl.shinigami.afvalbak;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class KMLHandler extends DefaultHandler {

	public static List<Placemark> placemarks;
	private Placemark currentPlacemark;

	boolean inName;
	boolean inDesc;
	boolean inStyle;
	boolean inCoor;
	boolean inPoint;

	@Override
	public void startDocument() throws SAXException {

		placemarks = new ArrayList<Placemark>();

	}

	@Override
	public void endDocument() throws SAXException {

	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		super.startElement(uri, localName, qName, attributes);

		if (localName.equalsIgnoreCase("Placemark")) {

			this.currentPlacemark = new Placemark();
		} else if (localName.equalsIgnoreCase("name")
				&& currentPlacemark != null) {
			if (currentPlacemark != null) {
				inName = true;
			}

		} else if (localName.equalsIgnoreCase("description")) {
			if (currentPlacemark != null) {
				inDesc = true;
			}

		} else if (localName.equalsIgnoreCase("styleUrl")) {
			if (currentPlacemark != null) {
				inStyle = true;
			}

		} else if (localName.equalsIgnoreCase("coordinates")) {
			if (currentPlacemark != null) {
				inCoor = true;
			}

		}

	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		super.endElement(uri, localName, qName);

		if (this.currentPlacemark != null) {

			if (localName.equalsIgnoreCase("Placemark")) {
				placemarks.add(currentPlacemark);
			}
		}

	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		super.characters(ch, start, length);

		String s = (new String(ch).substring(start, start + length));

		if (inName) {
			currentPlacemark.setName(s);
			inName = false;

		} else if (inDesc) {
			currentPlacemark.setDescription(s);
			inDesc = false;
		}

		else if (inStyle) {
			currentPlacemark.setStyleUrl(s);
			inStyle = false;

		} else if (inCoor) {
			currentPlacemark.setCoordinates(s);
			inCoor = false;
		}

	}

	public List<Placemark> getPlaceMarks() {
		return placemarks;

	}
}
